package cat.joronya.supermanager.europeo.privateleague;

import java.util.ArrayList;

import cat.joronya.supermanager.europeo.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Adapter for leagues list.
 */
public class LeagueListAdapter extends BaseAdapter 
{
	private LayoutInflater mInflater;
	private PrivateLeague[] privateLeagues;
	private int rowLayoutName;

	public LeagueListAdapter(Context context, ArrayList<PrivateLeague> pls, int rowLayoutName) 
	{
		 mInflater = LayoutInflater.from(context);
		 this.privateLeagues = new PrivateLeague[pls.size()];
		 int i = 0;
		 for(PrivateLeague pl: pls)
		 {
			 this.privateLeagues[i++] = pl;
		 }
		 this.rowLayoutName = rowLayoutName;
	 }

	public int getCount() 
	{
		return privateLeagues.length;
	}

	public Object getItem(int position) 
	{
		return position;
	}

	public long getItemId(int position) 
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) 
	{
		ViewHolder holder;
		if (convertView == null) 
		{
			convertView = mInflater.inflate(rowLayoutName, null);
			
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.private_league_list_row_name);
			holder.members = (TextView) convertView.findViewById(R.id.private_league_list_row_members);
			 
			convertView.setTag(holder);
		 } else {
			 holder = (ViewHolder) convertView.getTag();
		 }
		 
		 holder.name.setText(privateLeagues[position].getName());
		 holder.members.setText(privateLeagues[position].getNumberOfTeams());
		 
		 return convertView;
	}

	static class ViewHolder 
	{
		 TextView name;
		 TextView members;
	}
}
